#!/bin/bash

echo "Content-Type:text/html;charset=utf-8"
echo
#加载功能模块
source func/env.sh
source func/loginCheck.sh

source func/controller.sh

source func/loginCheck.sh
source func/tools.sh
source func/db.sh

#写日志
log INFO "请求信息"
query_string=$(tee)
checkLogin token

#传入动作
action=$(get_key action)
if [ $REQUEST_METHOD = "POST" ]; then
    action=$(echo $QUERY_STRING | sed 's/&/\n/g' | grep -w "action" | awk -F '=' '{print $2}')
fi

#登录
# token=$(echo $REQUEST | jq ".token" | sed 's/"//g')
# if [ $token == "1111" ]; then
#     echo "登录失败"
#     exit 255
# fi
case $action in
add_group)
    name=$(post_key $query_string name)
    describe=$(post_key $query_string describe)
    sql="insert hostgroup set  Name=\"$name\",Descrs=\"$describe\";"
    logInput Write "  添加数据  $sql"
    DataHanle $sql
    ;;

add_template)
    # name=$(post_key $query_string name)
    # check=$(post_key $query_string check)
    # makefile=$(post_key $query_string makefile)
    # playbook=$(post_key $query_string playbook)

    # name=$(printf $(echo -n "$query_string" | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g;s/+/ /g'))
    # check=$(printf $(echo -n "$check" | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g;s/+/ /g'))
    # makefile=$(printf $(echo -n "$makefile" | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g;s/+/ /g'))
    # playbook=$(printf $(echo -n "$playbook" | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g;s/+/ /g'))
    # echo $playbook
    echo "===$query_string"

    # sql="insert template set  name=\"$name\",\`check\`=\"$check\",makefile=\"$makefile\",playbook=\"$playbook\";"
    # echo $sql
    # DataHanle $sql
    ;;

add_host)

    user=$(post_key $query_string user)
    password=$(post_key $query_string password)
    port=$(post_key $query_string port)
    hostname=$(post_key $query_string hostname)
    group=$(post_key $query_string group)
    host=$(post_key $query_string host)
    if [ -z $group ]; then
        echo "组为空添加失败"
    fi
    sql="insert host set user=\"$user\",password=\"$password\",port=\"$port\",hostname=\"$hostname\",\`group\`=\"$group\",host=\"$host\" ;"
    DataHanle $sql

    ;;

add_head)

    user=$(post_key $query_string user)
    id=$(post_key $query_string id)
    userPng=$(post_key $query_string userPng)
    static=$(printf $(echo -n "$userPng" | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')"\n")
    sql="update user set \`Png\`=\"/$static\" where id=\"$id\";"
    echo $sql
    DataHanle $sql

    ;;
show_host_info)
    group=$(get_key group)
    Callback() {
        eval "cat <<EOF
                      <tr>
    <td>$1</td>
    <td>$2</td>
    <td>$4</td>
    <td>$5</td>
    <td>$6</td>
    <td>$7</td>
</tr>
EOF"
    }
    table_host=$(getData Callback "select * from host where \`group\`=\"$group\"")
    View=$(eval "cat <<EOF
                                $(<template/host-home.html)
EOF")

    eval "cat <<EOF
            $(<template/base.html)
EOF"

    ;;
delete)
    table=$(get_key db)
    id=$(get_key id)
    deleteDb $table $id

    ;;

updateUserinfo)
    echo "修改头像预留"
    username=$(get_key username)
    echo $username
    Callback() {
        id=$1
        username=$2

        png=$(ls -l static/img/user* | awk '{print "<option value="$9">"$9"</option>"}')

        eval "cat <<EOF
                            $(<form/up_head.html)
EOF"
    }
    echo "更新$username"

    View=$(getData Callback "select id,username,png from \`user\` where \`username\`=\"$username\"")
    eval "cat <<EOF
    $(<template/base.html)
EOF"

    ;;

get_form_host_info)
    echo "更新"

    table=$(get_key db)
    id=$(get_key id)
    echo $table $id

    Callback() {
        id=$1
        user=$2
        password=$3
        port=$4
        hostname=$5
        group=$6
        host=$7
        eval "cat <<EOF
                        $(<form/up_host.html)
EOF"
    }

    View=$(getData Callback "select * from host where id=$id")
    eval "cat <<EOF
$(<template/base.html)
EOF"

    ;;
up_host)
    id=$(post_key $query_string id)

    user=$(post_key $query_string user)
    password=$(post_key $query_string password)
    port=$(post_key $query_string port)
    hostname=$(post_key $query_string hostname)
    group=$(post_key $query_string group)
    host=$(post_key $query_string host)

    sql="update host set user=\"$user\",password=\"$password\",port=\"$port\",hostname=\"$hostname\",\`group\`=\"$group\",host=\"$host\"  where id=\"$id\";"
    DataHanle $sql

    ;;
exec_task)
    roots=$(pwd)
    template=$(post_key $query_string template)
    content=$(post_key $query_string content)
    group=$(post_key $query_string group)
    #生成变量配置
    #host生成
    #.env生成
    echo [$group] >task/$template/host

    Callback() {
        id=$1
        user=$2
        password=$3
        port=$4
        hostname=$5
        group=$6
        host=$7
        eval "cat <<EOF          
            {
                \"user\": \"$user\",
                    \"host\": \"$host\"
            },
EOF"
        echo "$host id=$id user=$user password=$password port=$port hostname=$hostname" >>task/$template/host

    }
    host_config=$(getData Callback "select * from host WHERE \`group\` = \"$group\"")
    echo -e $host_config | sed 's/,$/]/g;s/^/[/g' >task/$template/Env.json
    cd /tmp/
    file=$(mktemp task.XXXXXXXXXX)
    Time=$(date +"%Y-%m-%d %R")

    screen -S $file -m -d bash -c "cd   ${roots}/task/$template/ && make deploy  >/tmp/$file"
    sql="insert task set task=\"$file\",template=\"$template\",\`group\`=\"$group\",\`time\`=\"$Time\",\`content\`=\"$content\";"
    TaskHanle $sql

    ;;
show_task_result)
    task=$(get_key $query_string task)
    result=$(cat "/tmp/$task")
    View=$(eval "cat <<EOF
                            $(<template/task-result.html)
EOF")
    eval "cat <<EOF
    $(<template/base.html)
EOF"
    ;;
rollBack)
    echo "回滚"
    project=$(get_key project)
    version=$(get_key version)
    echo $project $version
    ;;
controller)
    status=$(get_key status)
    host=$(get_key host)
    project=$(get_key project)

    upadte_nginx_Config
    ;;
login)
    checkLogin password

    # username=$(post_key $query_string username)
    # password=$(post_key $query_string password)
    # u=$(echo -n $username | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')
    # p=$(echo -n $password | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g' | md5sum | awk '{print $1}')
    # file=$(mktemp /tmp/user.XXXXXXXXXX)
    # db_connect "SELECT \`id\` FROM \`user\` WHERE username = \"$u\" AND \`password\` = \"$p\";" | sed -e '1d' >$file
    # if [ ! -z $(cat $file) ]; then
    #     View=$(eval "cat <<EOF
    #                             $(<template/login_cok.html)
    # EOF")
    #     eval "cat <<EOF
    #     $(<template/base.html)
    # EOF"
    # else
    #     echo "<script>alert(\"登录失败账号密码错误\");window.location.href=\"login.cgi\"</script>"
    # fi
    # rm -rf $file
    ;;
*)

    echo "传入登录方法$action"

    ;;

esac
